package com.ibangnuo.framework.newLandpay.impl.request;

import com.alibaba.fastjson.JSONObject;
import com.ibangnuo.framework.newLandpay.api.NewLandpayRequest;
import com.ibangnuo.framework.newLandpay.impl.common.ResponseClient;
import com.ibangnuo.framework.newLandpay.impl.common.api.ResponseClientApi;
import com.ibangnuo.framework.newLandpay.impl.response.NewLandMercRegistResponse;
import com.ibangnuo.framework.newLandpay.util.HttpParamsUtils;
import com.ibangnuo.framework.newLandpay.util.MD5;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.Map;

/**
 * @Author: wwy
 * @Date: 2020-01-10 10:03
 */
@Data
@NoArgsConstructor
public class NewLandMercRegistRequest implements NewLandpayRequest {
    private static Logger LOGGER = LoggerFactory.getLogger(NewLandMercRegistRequest.class);
    private Map<String,String> bizContent;//不进行加密的参数
    private Map<String,String> pre;//进行加密的参数
    private final String URL = "http://sandbox.starpos.com.cn/emercapp";

    @Override
    public NewLandMercRegistResponse execute(String key) {
        pre.put("serviceId", "6060601");//交易码
        String preStr = HttpParamsUtils.buildPayValues(pre, false, true);
        String sign = MD5.sign(preStr, key, "UTF-8");
        pre.put("signValue",sign);
        bizContent.putAll(pre);
        ResponseClientApi responseClientApi = new ResponseClient(URL);
        String bizContentJson = JSONObject.toJSONString(bizContent);
        JSONObject jsonObject = JSONObject.parseObject(bizContentJson);
        String response = responseClientApi.request(jsonObject);
        LOGGER.info("新大陆接口响应结果{}",response);
        Map<String,String> map = JSONObject.parseObject(response, Map.class);
        if(map !=null){
            NewLandMercRegistResponse newLandMercRegistResponse = new NewLandMercRegistResponse();
            newLandMercRegistResponse.setLogNo(map.get("logNo"));
            //newLandMercRegistResponse.setMercId(map.get("mercId"));
            newLandMercRegistResponse.setMsgCd(map.get("msg_cd"));
            newLandMercRegistResponse.setMsgDat(map.get("msg_dat"));
            newLandMercRegistResponse.setStoeId(map.get("stoeId"));
            newLandMercRegistResponse.setSignValue(map.get("signValue"));
            newLandMercRegistResponse.setBody(JSONObject.toJSONString(map));
            return newLandMercRegistResponse;
        }
        return null;
    }
}
